<?php
    /**
    * User.php
    * 
    * @author Osuvaldo Ramos <xdracox@gmail.com>
    * @copyright Copyright (c) 2008, Osuvaldo Ramos
    * @license http://www.opensource.org/licenses/bsd-license.php
    * @package Atman
    */
    
    /**
    * Class that represents a single user row in the users table of the
    * database.
    * @package Atman
    */
    class AT_User extends AT_DB_Row
    {
        /**
        * Stores an array of the table row information.
        * @var array
        */
        protected $data = array();
        
        // table information
        private $id;
        private $email;
        private $password;
        private $firstName;
        private $lastName;
        private $joinDate;
        private $status;
        private $accountType;
        private $admin;
        
        /**
        * Loads the user information according to the userID supplied.
        * @param int $userID The userID of the user to load.
        */
        public function __construct($userID)
        {
            parent::__construct('users');
            
            $sql = 'SELECT *
                    FROM '.$this->tableName.'
                    WHERE id = '.$userID;
            
            $result = $this->mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL SELECT query failed: '.$sql;
                $this->reportError($msg, PEAR_LOG_CRIT);
            }
            
            if ( $result->numRows() != 1 )
            {
                $msg = '$result->numRows() != 1';
                $this->reportError($msg, PEAR_LOG_CRIT);
            }
            
            $row = $result->fetchRow();
            
            $this->id = $row['id'];
            $this->email = $row['email'];
            $this->password = $row['password'];
            $this->firstName = $row['firstname'];
            $this->lastName = $row['lastname'];
            $this->joinDate = $row['joindate'];
            $this->status = $row['status'];
            $this->accountType = $row['accounttype'];
            $this->admin = $row['admin'] != 0;
        }
        
        /**
        * Updates the information for the current user.
        * @return void
        */
        public function update()
        {
            $sql = 'UPDATE %s
                        SET email = "%s", password = "%s",
                            firstName = "%s", lastName = "%s",
                            joinDate = %d, status = "%s"
                        WHERE id = %d';
            $sql = sprintf($sql, $this->tableName, 
                $this->email, $this->password, $this->firstName,
                $this->lastName, $this->joinDate, $this->status, $this->id);
            
            $result = $this->mdb2->exec($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL UPDATE failed: '.$result->getMessage();
                $this->reportError($msg, PEAR_LOG_ALERT);
            }
        }
        
        /**
        * Sets the user's email address to a different one. WARNING: This method
        * does not check the validity of the email address!
        * @param string $email The new email address.
        * @return void
        */
        public function setEmail($email)
        {
            $this->email = $email;
        }
        
        /**
        * Because the password in the database is hashed, direct access to it
        * is unsafe. This method allows a password to be hashed and then
        * saved in the database.
        * @param string $password The plain-text password to hash.
        * @return void
        */
        public function setPassword($password)
        {
            $password = $this->hashPassword($password);
            $this->password = $password;
        }
        
        /**
        * Changes the users' first name field.
        * @param string $firstName The user's new first name.
        * @return void
        */
        public function setFirstName($firstName)
        {
            $this->firstName = $firstName;
        }
        
        /**
        * Changes the user's last name field.
        * @param string $lastName the user's new last name.
        * @return void
        */
        public function setLastName($lastName)
        {
            $this->lastName = $lastName;
        }
        
        /**
        * Sets the user's status.
        * @param string $status The new status.
        * @return void
        */
        public function setStatus($status)
        {
            $this->status = $status;
        }
        
        /**
        * Sets the user's account type.
        * @param string $type The new account type.
        * @return void
        */
        public function setAccountType($type)
        {
            $this->accountType = $type;
        }
        
        /**
        * Sets the user's admin status.
        * @param bool $admin True or false.
        * @return void
        */
        public function setAdmin($admin)
        {
            $this->admin = $admin;
        }
        
        /**
        * Returns the user's numerical ID.
        * @return int
        */
        public function getID()
        {
            return $this->id;
        }
        
        /**
        * Returns the user's email.
        * @return string
        */
        public function getEmail()
        {
            return $this->email;
        }
        
        /**
        * Simple accessor method for the hashed password.
        * @return string
        */
        public function getPassword()
        {
            return $this->password;
        }
        
        /**
        * Returns the user's first name.
        * @return string
        */
        public function getFirstName()
        {
            return $this->firstName;
        }
        
        /**
        * Returns the user's last name.
        * @return string
        */
        public function getLastName()
        {
            return $this->lastName;
        }
        
        /**
        * Returns the user's join date which is in time() format.
        * @return int
        */
        public function getJoinDate()
        {
            return $this->joinDate;
        }
        
        /**
        * Returns the user's status.
        * @return string
        */
        public function getStatus()
        {
            return $this->status;
        }
        
        /**
        * Returns the user's account type.
        * @return string
        */
        public function getAccountType()
        {
            return $this->accountType;
        }
        
        /**
        * Returns the user's admin status.
        * @return bool
        */
        public function getAdmin()
        {
            return $this->admin;
        }
        
        /**
        * This method applies a salt and hashes a string to fit the requirements
        * of a hashed password.
        * @param string $str The string to hash.
        * @return string
        */
        public static function hashPassword($str)
        {
            $hash = md5(AT_SALT2.md5(md5(AT_SALT1).md5($str)));
            return $hash;
        }
        
        /**
        * __destruct()
        */
        public function __destruct()
        {
            parent::__destruct();
        }
    }
?>
